import zcoIMEvent from './event';

class IMState {
	constructor() {
		this.width = window.innerWidth;
		this.height = window.innerHeight;
		this.type = 'mobile';
		this.agent = null;
		this.client = null;
		this.chatMessage = [];
		this.mobileTitle = 'video';
		this.maxUploadFileSize = 1024000 * 50;
		this.showBusinessMessage = false;
		this.ignoreMessageType = []
	}

	setIgnoreMessageType (ignoreMessageType) {
		this.ignoreMessageType = ignoreMessageType
	}

	setShowBusinessMessage (showBusinessMessage) {
		this.showBusinessMessage = showBusinessMessage;
	}

	setIMView (width, height, type) {
		this.width = width;
		this.height = height;
		this.type = type;
	}

	setAgentData (agent) {
		this.agent = agent;
		zcoIMEvent.updateUserInfo(this.agent);
	}

	setClientData (client) {
		this.client = client;
		zcoIMEvent.updateUserInfo(this.client);
	}

	setChatMessage (message) {
		if (message instanceof Array) {
			let newMessage = this.setShowTime(message);
			newMessage.forEach(item => {
				this.chatMessage.push(item);
			});
			zcoIMEvent.updateChatMessage(this.chatMessage);
		} else {
			let endIndex = this.chatMessage.length - 1;
			if (this.chatMessage[endIndex]) {
				message.showTime = message.date - this.chatMessage[endIndex].date >= 300000;
			}
			this.chatMessage.push(message);
			zcoIMEvent.updateChatMessage(this.chatMessage);
		}
		setTimeout(() => {
			const current = document.getElementById('chat-message');
			if (current) {
				current.scrollTop = current.scrollHeight;
			}
		}, 100);
	}

	setShowTime = state => {
		let copyList = [...state];
		if (copyList.length !== 0) {
			let firstTime = copyList[0].date;
			copyList.forEach(item => {
				if (item.date - firstTime >= 300000) {
					firstTime = item.date;
					item.showTime = true;
				}
			});
		}
		return copyList;
	};
}

export default IMState;
